﻿using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using WebDAVSharp.Server;
using WebDAVSharp.Server.Stores.BaseClasses;

namespace AzureStorageLight
{
    public class BackupWebDavStoreItem : WebDavStoreItemBase
    {
        protected StorageItemManager Service
        {
            get
            {
                if (_service == null)
                {
                    _service = StorageItemManager.CreateStorageItemManager(StorageConfig.Instance);
                }
                return _service;
            }
        }
        private StorageItemManager _service;

        public CloudBlockBlob Snapshot{ get; private set; }

        public override string ItemPath
        {
            get
            {
                return base.Name;
            }
        }

        public new bool IsCollection
        {
            get
            {
                return base.Name.EndsWith("/");
            }
        }

        public BackupWebDavStoreItem(BackupWebDavStoreCollection parentCollection, CloudBlockBlob snapshot, string path)
            : base(parentCollection, path)
        {
            this.Snapshot = snapshot;
        }

        internal static string MakeSnapshotName(CloudBlockBlob item)
        {
            if (item.IsSnapshot)
            {
                var ext = Path.GetExtension(item.Name);
                return item.Name.Substring(0, item.Name.Length - ext.Length) + "_T" + item.SnapshotTime.Value.ToString("yyyyMMddHHmmssfff") + ext;
            }
            else
            {
                return item.Name;
            }
        }

    }
}
